<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/30
 * Time: 21:57
 */

namespace Xcx\Model;

class GoodsBargainDetailsModel extends BaseModel
{

    public $errMsg;

    public function getErrMsg()
    {
        return $this->errMsg;
    }

    /**
     * 获取砍价总量
     * @param $pid
     * @return mixed
     */
    public function getBargainCount($pid)
    {
        $count = $this->where(array('pid'=>$pid))->sum('cut_count');
        if ($count) return $count;
        return 0;
    }

    /**
     * 获取砍价详情列表
     * @param $pid
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getBargainDetailsList($pid)
    {
        $list =  $this->alias('a')
            ->field('a.*,b.nickname,b.header')
            ->join('left join t_users b on a.uid = b.id')
            ->where(array('pid'=>$pid))
            ->order(array('a.cut_count'=>'desc','a.add_time'=>'desc'))
            ->limit(5)
            ->select();
        if (is_array($list) && count($list) > 0) return getUploadImgDir($list,array('user_head'=>'header'));
        return false;
    }

    /**
     * 砍价
     * @param $orderNo
     * @return bool
     */
    public function doBargain($orderNo)
    {
        if ($this->checkBargainRight($orderNo)) {
            $res = $this->addBargainRecord($orderNo);
            if ($res) {
                return true;
            } else {
                $this->errMsg = '砍价失败';
                return true;
            }
        } else {
            return false;
        }
    }

    /**
     * 添加砍价信息详情
     * @param $orderNo
     * @return mixed
     */
    private function addBargainRecord($orderNo)
    {
        $GoodsBargain = new GoodsBargainModel();
        $bargainInfo = $GoodsBargain->getOrderBargainInfo($orderNo);

        $cutCount = $this->getCutCount();

        $bargainCount = $this->getBargainCount($bargainInfo['id']);

        $remainCount = $bargainInfo['limit_cut'] - $bargainCount;
        if ($remainCount < $cutCount) {
            $cutCount = $remainCount;
        }

        $data['pid'] = $bargainInfo['id'];
        $data['uid'] = $this->uid;
        $data['cut_count'] = $cutCount;
        $data['add_time'] = time();

        return $this->data($data)->add();
    }

    public function getCutCount()
    {
        $arr = array(
            array('id'=>1,'name'=>'1','v'=>90),
            array('id'=>2,'name'=>'2','v'=>8),
            array('id'=>3,'name'=>'3','v'=>2)
        );

        $data = get_rand($arr);
        if ($data['id']  == 1) {
            $cutCount = rand(1,3);
        } else if ($data['id'] == 2) {
            $cutCount = rand(4,7);
        } else {
            $cutCount = rand(8,10);
        }
        return $cutCount;
    }

    /**
     * 检查用户砍价权限
     * @param $orderNo
     * @return bool
     */
    private function checkBargainRight($orderNo)
    {
        $GoodsBargain = new GoodsBargainModel();
        $bargainInfo = $GoodsBargain->getOrderBargainInfo($orderNo);
        //判断是否有砍价记录
        if (!$bargainInfo) {
            $this->errMsg = '无效砍价订单';
            return false;
        }
        //判断砍价金额
        $bargainCount = $this->getBargainCount($bargainInfo['id']);
        if ($bargainCount > $bargainInfo['limit_cut']) {
            $this->errMsg = '砍价金额已达上限';
            return false;
        }
        //检查用户是否有砍价记录
        if ($this->checkUserIsBargain($bargainInfo['id'])) {
            $this->errMsg = '您已完成过一次砍价';
            return false;
        }
        return true;
    }

    /**
     * 检查用户是否有砍价记录
     * @param $pid
     * @return array|false
     */
    public function checkUserIsBargain($pid)
    {
        $res = $this->where(array('pid'=>$pid,'uid'=>$this->uid))->find();
        if (is_array($res) && count($res) > 0) return true;
        return false;
    }
}